<?php
require_once 'TVB/pms/pdes/common/util/Unpublished.class.php';

class EpisodeSwitchAction extends BaseAction 
{
	public $id;
	public $programme_id;
	public $onair_episode_no;
	public $original_episode_no;
	public $average_rating;
	public $first_network_codes;
	public $first_time_onair;
	public $publish_datetime;
	public $expire_datetime;
	public $other_language_episodes;
	public $highlighted_cast_roles;
	public $casts;
	public $available_tag_types;
	public $tags;
	public $folders;
	public $status;
	public $created_at;
	public $created_by;
	public $modified_at;
	public $modified_by;

	public $language_id;
	public $title;
	public $description;
	public $description_short;
	public $editor_message;
	public $ut;

	public function execute(){
		if (count($this->_errors) > 0)
		{
			return "filter-error";
		}

		if (isset($_REQUEST['delete']))
		{
			return $this->delete_episode();
		}
		elseif (isset($_REQUEST['clone']))
		{
			return $this->clone_main();
		}
		elseif (isset($_REQUEST['proof']))
		{
			return $this->proof_episode();
		}
		elseif (isset($_REQUEST['recover']))
		{
			return	$this->recover_episode();
		}
		elseif (isset($_REQUEST['edit']))
		{
			return $this->edit_episode();
		}
		elseif (isset($_REQUEST['unpublish']))
		{
			return $this->unpublish_episode();
		}
        elseif (isset($_REQUEST['unpublish_premium']))
		{
			return $this->unpublish_premium_episode();
		}
	}    

	/**
	 * clone main
	 */
	private function clone_main()
	{

		$clone_id = $this->clone_episode();
		$this->clone_episode_language($clone_id);
		$this->clone_multiple_language($clone_id);

		$this->setData('type', 'episode');
		$this->setData('id', $clone_id);
		$this->setData('act', 'clone');
		$this->setData('step', 2);
		return 'clone';
	}

	/**
	 * clone episode
	 */
	private function clone_episode()
	{
		$episode = new episode();
		$episode_clone = $episode->getDetails($this->id);
		$episode->set($episode_clone);
		$episode->created_at = date("Y-m-d H:i:s", $_SERVER['REQUEST_TIME']);
		$episode->created_by = CommonUtils::get_user();
		$episode->folders = CommonUtils::init_folder("episode", $this);
		$episode->available_tag_types = CommonUtils::get_allow_tags("episode");
		$episode->highlighted_cast_roles = CommonUtils::get_highlight_role("episode");
		$episode->status = AC::STATUS_NEW;
		$episode->onair_episode_no = '';

		$clone_id = Model::$dbo['w']->save($episode);
		if ($clone_id > 0)
		{
			$this->addInfo("episode:create:".$clone_id, "add episode success", json_encode($episode));
			return $clone_id;
		}
		else
		{
			$this->addError("episode:create:".$clone_id, "add episode error", json_encode($episode));
			return false;
		}
	}

	/**
	 * clone episode language
	 */
	private function clone_episode_language($clone_id)
	{
		$episode_language = new episode_language();
		$episode_detail = $episode_language->getLangEpisodeByID($this->id,language::getLanguageID(AC::DEFAULT_LANGUAGE));
		$episode_language->set($episode_detail);
		$episode_language->episode_id = $clone_id;
		$episode_language->language_id = language::getLanguageID(AC::DEFAULT_LANGUAGE);
		$ret = Model::$dbo['w']->save($episode_language);
		if ($ret >= 0)
		{
			$this->addInfo("episode-language:create:".$clone_id, "add episode language success", json_encode($episode_language));
			return true;
		}
		else
		{
			$this->addError("episode-language:create:".$clone_id, "add episode language error", json_encode($episode_language));
			return false;
		}
	}

	/**
	 * clone multiple language
	 */
	private function clone_multiple_language($clone_id)
	{
		$episode_language = new episode_language();
		$episode_detail = $episode_language->getLangEpisodeByID($this->id,language::getLanguageID(AC::LANGUAGE_EN));
		$episode_language->set($episode_detail);
		$episode_language->language_id = language::getLanguageID(AC::LANGUAGE_EN);
		$episode_language->episode_id= $clone_id;

		$ret = Model::$dbo['w']->save($episode_language);
		if ($ret >= 0)
		{
			$this->addInfo("episode-language:".AC::LOG_OP_UPDATE.":".$clone_id, "update episode language success", json_encode($episode_language));
			return true;
		}
		else
		{
			$this->addError("episode-language:".AC::LOG_OP_UPDATE.":".$clone_id, "update episode language error", json_encode($episode_language));
			return false;
		}
	}

	/**
	 * delete episode
	 */
	private function delete_episode()
	{
		try
		{
			$publish = publish_log::getPublishLog('episode', $this->id);
			if (isset($publish->status) && $publish->status == AC::STATUS_PUBLISH)
			{
				$this->setData('step', '2');
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0107');
				return 'logic-error';
			}

			$episode = new episode();
			$episode = $episode->get($this->id);
			$episode->status = AC::STATUS_DELETED;
			$episode->modified_at = date("Y-m-d H:i:s");
			$episode->modified_by = CommonUtils::get_user();
			$update_id = Model::$dbo['w']->save($episode);
			if ($update_id > 0)
			{
				# change_log
				change_log::delEpisodeLog($this->id);
				$this->addInfo("episode:".AC::LOG_OP_DELETE.":".$this->id, "episode delete success", json_encode($episode));
			}
			else
			{
				$this->addError("episode:".AC::LOG_OP_DELETE.":".$this->id, "episode delete error", json_encode($episode));
			}

			$this->setData('type', 'episode');
			return 'delete';
		}
		catch(Exception $e)
		{
			error_log("delete episode: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}

	/**
	 * edit episode
	 */
	private function edit_episode()
	{
		try{
			$episode = new episode();
			$episode = $episode->get($this->id);
			if ($episode->status == AC::STATUS_DELETED)
			{
				$this->setData('step', '2');
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0108');
				return 'logic-error';
			}

			$this->setData('id', $this->id);
			$this->setData('type', 'episode');
			$this->setData('step', 2);
			$this->setData('act', 'edit');

			return 'edit';
		}
		catch(Exception $e)
		{
			error_log("edit episode: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}

	/**
	 * proof read
	 */
	private function proof_episode()
	{
		Model::$dbo['w']->beginTransaction();
		try{
			$episode = new episode();
			$episode = $episode->get($this->id);
			if ($episode->status == AC::STATUS_DELETED)
			{
				$this->setData('step', '2');
				$this->setData('act', 'view');
				$this->setData('id', $this->id);
				$this->setData('error_code', '0106');
				return 'logic-error';
			}
		
			$proof_flag = episode::publish($this->id);

			if( $proof_flag !== true ){
				$this->setData('error_code', $proof_flag);
				$this->setData('id', $this->id);
				$this->setData('type', 'episode');
				$this->setData('act', 'view');
				$this->setData('step', 2);
				return 'logic-error';
			}
			else
			{
          	    Model::$dbo['w']->commit();
				$this->setData('type', 'episode');
				return 'proof';
			}
		}
		catch(Exception $e)
		{
			error_log("Proof Episode Fails: {$this->id}");
			error_log("Exception: " . var_export($e,1));

			Model::$dbo['w']->rollBack();

			return 'system-error';
		}
	}

	/**
	 * recover episode
	 */
	private function recover_episode()
	{
		try{
			$episode = new episode();
			$episode = $episode->get($this->id);
			$episode->status = AC::STATUS_NEW;
			$episode->modified_at = date("Y-m-d H:i:s");
			$episode->modified_by = CommonUtils::get_user();
			$update_id = Model::$dbo['w']->save($episode);
			if ($update_id > 0)
			{
				$this->addInfo("episode:".AC::LOG_OP_RECOVER.":".$this->id, "episode recover success", json_encode($episode));
			}
			else
			{
				$this->addError("episode:".AC::LOG_OP_RECOVER.":".$this->id, "episode recover error", json_encode($episode));
			}

			$this->setData('type', 'episode');
			return 'recover';
		}
		catch(Exception $e)
		{
			error_log("recover episode: {$this->id}" . var_export($e,1));

			return 'system-error';
		}
	}


	///** Merge to unpublish_episode
    private function unpublish_premium_episode()
    {
        Model::$dbo['w']->beginTransaction();
		try{

			$episode = new episode();
			$episode = $episode->get($this->id);
			$log_id  = CommonUtils::save_log('episode', $this->id, AC::LOG_OP_PREMIUM_UNPUBLISHING , json_encode($episode));
			$unpublish_flag = P_Unpublished::send('episode', $this->id, $episode->programme_id, $log_id);

			Model::$dbo['w']->commit();
			$this->setData('type', 'episode');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("Unpublish Episode Fails: $this->id");
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();
	
			return 'system-error';
		}
    }
	// */
	/**
	 * unpublish episode
	 */ 
	private function unpublish_episode()
	{
		Model::$dbo['w']->beginTransaction();
		try{

			$episode = new episode();
			$episode = $episode->get($this->id);
			$log_id  = CommonUtils::save_log('episode', $this->id, AC::LOG_OP_UNPUBLISHING, json_encode($episode));
			Unpublished::send('episode', $this->id, $episode->programme_id, $log_id);

			// donald 2011-03-21
			//if(CommonUtils::checkLastPublishStatus('episode',$this->id,AC::STATUS_PUBLISH_PREMIUM)){
			//	#$log_id  = CommonUtils::save_log('episode', $this->id, AC::LOG_OP_PREMIUM_UNPUBLISHING , json_encode($episode));
			//	P_Unpublished::send('episode', $this->id, $episode->programme_id, $log_id);
			//}

			Model::$dbo['w']->commit();
			$this->setData('type', 'episode');
			return 'unpublish';
		}
		catch(Exception $e)
		{
			error_log("Unpublish Episode Fails: $this->id");
			error_log("Exception: " . var_export($e, 1));

			Model::$dbo['w']->rollBack();
	
			return 'system-error';
		}
	}
}
?>


